package org.springsource.dao.impl;

import javax.inject.Inject;
import javax.persistence.NamedNativeQuery;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import org.springsource.dao.AccountDao;
import org.springsource.domain.Account;

@NamedNativeQuery(name = "callStoredProcedure", query = "{call sp_getAccountByUserName(:username)};", resultClass = Account.class)
@Repository
public class AccountDaoImpl  extends AbstractJpaDao<Account> implements AccountDao  {
	private static final Logger logger = LoggerFactory
			.getLogger(AccountDaoImpl.class);

	
	
	private final PasswordEncoder passwordEncoder;
	
	@Inject
	public AccountDaoImpl(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}
	@Override
	public void save(Account account) {
		logger.info("in save account dao impl");
		account.setPassword(passwordEncoder.encode(account.getPassword()));
		super.save(account);
		logger.info("pass"+account.getPassword());
	}

	@Override
	public Account getAccount(String username) {
		/*
		 * Account acc = entityManager.createQuery(
		 * "SELECT a FROM Account a WHERE a.username = :username",
		 * Account.class).setParameter("username", username).getSingleResult();
		 * return acc;
		 */

		logger.info("size:");
		Query query = entityManager.createNativeQuery(
				"{call sp_getAccountByUserName(:username)};", Account.class)
				.setParameter("username", username);

		return (Account) query.getSingleResult();

	}
	@Override
	public void saveOrUpdate(Account account) {
		// TODO Auto-generated method stub
		
	}
}
